package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharShrineList;
import org.gdstash.character.GDCharUID;
import org.gdstash.db.DBShrine;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractShrinePane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class GDCharShrineAct1Pane extends GDAbstractShrinePane {
    private JCheckBox cbBurialHill;
    private JCheckBox cbDevilsAquifier;
    private JCheckBox cbWightmireCavern;
    private JCheckBox cbFloodedPassage;
    private JCheckBox cbBurialCave;
    private JCheckBox cbHallowedHill;
    private JCheckBox cbBurrwitchEstates;
    private JCheckBox cbUndergroundTransit;
    private JCheckBox cbCraigsCrags;
    private JCheckBox cbGuardianDreeg;
    private JCheckBox cbTempleThree;
    private int difficulty;
    private boolean changed;

    public GDCharShrineAct1Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbBurialHill).addComponent(this.cbDevilsAquifier).addComponent(this.cbWightmireCavern).addComponent(this.cbFloodedPassage).addComponent(this.cbBurialCave).addComponent(this.cbHallowedHill).addComponent(this.cbBurrwitchEstates).addComponent(this.cbUndergroundTransit).addComponent(this.cbCraigsCrags).addComponent(this.cbGuardianDreeg).addComponent(this.cbTempleThree));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbBurialHill)).addGroup(layout.createParallelGroup().addComponent(this.cbDevilsAquifier)).addGroup(layout.createParallelGroup().addComponent(this.cbWightmireCavern)).addGroup(layout.createParallelGroup().addComponent(this.cbFloodedPassage)).addGroup(layout.createParallelGroup().addComponent(this.cbBurialCave)).addGroup(layout.createParallelGroup().addComponent(this.cbHallowedHill)).addGroup(layout.createParallelGroup().addComponent(this.cbBurrwitchEstates)).addGroup(layout.createParallelGroup().addComponent(this.cbUndergroundTransit)).addGroup(layout.createParallelGroup().addComponent(this.cbCraigsCrags)).addGroup(layout.createParallelGroup().addComponent(this.cbGuardianDreeg)).addGroup(layout.createParallelGroup().addComponent(this.cbTempleThree));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbBurialHill, this.cbDevilsAquifier);
        layout.linkSize(0, this.cbBurialHill, this.cbWightmireCavern);
        layout.linkSize(0, this.cbBurialHill, this.cbFloodedPassage);
        layout.linkSize(0, this.cbBurialHill, this.cbBurialCave);
        layout.linkSize(0, this.cbBurialHill, this.cbHallowedHill);
        layout.linkSize(0, this.cbBurialHill, this.cbBurrwitchEstates);
        layout.linkSize(0, this.cbBurialHill, this.cbUndergroundTransit);
        layout.linkSize(0, this.cbBurialHill, this.cbCraigsCrags);
        layout.linkSize(0, this.cbBurialHill, this.cbGuardianDreeg);
        layout.linkSize(0, this.cbBurialHill, this.cbTempleThree);
        layout.linkSize(1, this.cbBurialHill, this.cbDevilsAquifier);
        layout.linkSize(1, this.cbBurialHill, this.cbWightmireCavern);
        layout.linkSize(1, this.cbBurialHill, this.cbFloodedPassage);
        layout.linkSize(1, this.cbBurialHill, this.cbBurialCave);
        layout.linkSize(1, this.cbBurialHill, this.cbHallowedHill);
        layout.linkSize(1, this.cbBurialHill, this.cbBurrwitchEstates);
        layout.linkSize(1, this.cbBurialHill, this.cbUndergroundTransit);
        layout.linkSize(1, this.cbBurialHill, this.cbCraigsCrags);
        layout.linkSize(1, this.cbBurialHill, this.cbGuardianDreeg);
        layout.linkSize(1, this.cbBurialHill, this.cbTempleThree);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_ACT1"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbBurialHill == null) {
            (this.cbBurialHill = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBurialHill.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_BURIAL_HILL"));
        this.cbBurialHill.setFont(fntCheck);
        if (this.cbDevilsAquifier == null) {
            (this.cbDevilsAquifier = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDevilsAquifier.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_DEVILS_AQUIFIER"));
        this.cbDevilsAquifier.setFont(fntCheck);
        if (this.cbWightmireCavern == null) {
            (this.cbWightmireCavern = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbWightmireCavern.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_WIGHTMIRE_CAVERN"));
        this.cbWightmireCavern.setFont(fntCheck);
        if (this.cbFloodedPassage == null) {
            (this.cbFloodedPassage = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbFloodedPassage.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_FLOODED_PASSAGE"));
        this.cbFloodedPassage.setFont(fntCheck);
        if (this.cbBurialCave == null) {
            (this.cbBurialCave = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBurialCave.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_BURIAL_CAVE"));
        this.cbBurialCave.setFont(fntCheck);
        if (this.cbHallowedHill == null) {
            (this.cbHallowedHill = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbHallowedHill.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_HALLOWED_HILL"));
        this.cbHallowedHill.setFont(fntCheck);
        if (this.cbBurrwitchEstates == null) {
            (this.cbBurrwitchEstates = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbBurrwitchEstates.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_BURRWITCH_ESTATES"));
        this.cbBurrwitchEstates.setFont(fntCheck);
        if (this.cbUndergroundTransit == null) {
            (this.cbUndergroundTransit = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbUndergroundTransit.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_UNDERGROUND_TRANSIT"));
        this.cbUndergroundTransit.setFont(fntCheck);
        if (this.cbCraigsCrags == null) {
            (this.cbCraigsCrags = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCraigsCrags.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_CRAIGS_CRAGS"));
        this.cbCraigsCrags.setFont(fntCheck);
        if (this.cbGuardianDreeg == null) {
            (this.cbGuardianDreeg = new JCheckBox()).setEnabled(false);
        }
        this.cbGuardianDreeg.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_GUARDIAN_OF_DREEG"));
        this.cbGuardianDreeg.setFont(fntCheck);
        if (this.cbTempleThree == null) {
            (this.cbTempleThree = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTempleThree.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_01_TEMPLE_OF_THE_THREE"));
        this.cbTempleThree.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbBurialHill.setSelected(selected);
        this.cbDevilsAquifier.setSelected(selected);
        this.cbWightmireCavern.setSelected(selected);
        this.cbFloodedPassage.setSelected(selected);
        this.cbBurialCave.setSelected(selected);
        this.cbHallowedHill.setSelected(selected);
        this.cbBurrwitchEstates.setSelected(selected);
        this.cbUndergroundTransit.setSelected(selected);
        this.cbCraigsCrags.setSelected(selected);
        this.cbGuardianDreeg.setSelected(selected);
        this.cbTempleThree.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getShrineList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbBurialHill.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_BURIAL_HILL);
        }
        if (this.cbDevilsAquifier.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_DEVILS_AQUIFIER);
        }
        if (this.cbWightmireCavern.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_WIGHTMIRE_CAVERN);
        }
        if (this.cbFloodedPassage.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_FLOODED_PASSAGE);
        }
        if (this.cbBurialCave.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_BURIAL_CAVE);
        }
        if (this.cbHallowedHill.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_HALLOWED_HILL);
        }
        if (this.cbBurrwitchEstates.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_BURRWITCH_ESTATES);
        }
        if (this.cbUndergroundTransit.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_UNDERGROUND_TRANSIT);
        }
        if (this.cbCraigsCrags.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_CRAIGS_CRAGS);
        }
        if (this.cbGuardianDreeg.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_GUARDIAN_DREEG);
        }
        if (this.cbTempleThree.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_01_TEMPLE_THREE);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        boolean expFG = GDStashFrame.expansionForgottenGods;
        if (gdc != null) {
            expFG = (expFG || gdc.isForgottenGodsChar());
        }
        DBShrine.Info info = null;
        info = DBShrine.getShrineInfo("devotionshrinea01.dbr", this.difficulty);
        this.cbBurialHill.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinea06.dbr", this.difficulty);
        this.cbDevilsAquifier.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinea02.dbr", this.difficulty);
        this.cbWightmireCavern.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinea04.dbr", this.difficulty);
        this.cbFloodedPassage.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinea03.dbr", this.difficulty);
        this.cbBurialCave.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen04.dbr", this.difficulty);
        this.cbHallowedHill.setEnabled(info.active);
        this.cbHallowedHill.setVisible(info.exists || expFG);
        info = DBShrine.getShrineInfo("devotionshrinea05.dbr", this.difficulty);
        this.cbBurrwitchEstates.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinea07.dbr", this.difficulty);
        this.cbUndergroundTransit.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinea08.dbr", this.difficulty);
        this.cbCraigsCrags.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrinen10.dbr", this.difficulty);
        this.cbGuardianDreeg.setEnabled(info.active);
        this.cbGuardianDreeg.setVisible(info.exists || expFG);
        info = DBShrine.getShrineInfo("devotionshrinen03.dbr", this.difficulty);
        this.cbTempleThree.setEnabled(info.active);
        this.cbTempleThree.setVisible(info.exists || expFG);
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRestoredShrinesList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_BURIAL_HILL)) {
                this.cbBurialHill.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_DEVILS_AQUIFIER)) {
                this.cbDevilsAquifier.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_WIGHTMIRE_CAVERN)) {
                this.cbWightmireCavern.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_FLOODED_PASSAGE)) {
                this.cbFloodedPassage.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_BURIAL_CAVE)) {
                this.cbBurialCave.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_HALLOWED_HILL)) {
                this.cbHallowedHill.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_BURRWITCH_ESTATES)) {
                this.cbBurrwitchEstates.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_UNDERGROUND_TRANSIT)) {
                this.cbUndergroundTransit.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_CRAIGS_CRAGS)) {
                this.cbCraigsCrags.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_GUARDIAN_DREEG)) {
                this.cbGuardianDreeg.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_01_TEMPLE_THREE)) {
                this.cbTempleThree.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharShrineAct1Pane.this.changed = true;
        }
    }
}
